package com.musicmall.controller.backend;

import com.musicmall.common.Const;
import com.musicmall.common.ServerResponse;
import com.musicmall.pojo.User;
import com.musicmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/manage/user")
public class UserManageController {

    @Autowired
    private IUserService iUserService;

    @GetMapping("login.do")
    public ServerResponse login(String username, String password, HttpSession session){
        ServerResponse response = iUserService.login(username, password);
        if(response.isSuccess()){
            User user = (User) response.getData();
            if(user.getRole()==Const.Role.ROLE_ADMIN){
                session.setAttribute(Const.CURRENT_USER,user);
                return response;
            }
            else{
                return ServerResponse.createByErrorMessage("不是管理员，无法登录");
            }
        }
        return response;

    }

}
